package com.park.web.controller.user;

import java.io.File;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.park.client.model.book.BookModel;
import com.park.client.model.user.UserModel;
import com.park.client.service.book.BookService;
import com.park.common.file.FileUtil;
import com.park.web.contant.Global;
import com.park.web.controller.BaseController;
import com.park.web.enums.FileTypeEnum;
import com.park.web.util.SessionUtil;
import com.park.web.util.pdf.PdfUtil;
import com.park.web.util.poi.PoiUtil;

@Controller
@RequestMapping("/user/book")
public class UserBookController extends BaseController{
	
	@Autowired
	private BookService bookService;
	
	@RequestMapping("bookList")
	public String bookList(HttpServletRequest request,HttpSession session){
		UserModel user = SessionUtil.getSessionUser(session);
		List<BookModel> books = bookService.selectByUserId(user.getId());
		request.setAttribute("books", books);
		return Global.USER_PATH+"resourcefile/bookList";
	}
	
	@RequestMapping("saveBook")
	public String saveBook(@RequestParam("bookPath") MultipartFile file,BookModel book,HttpServletRequest request,HttpSession session){
		if(file.isEmpty()){
			return null;
		}
		try {
			UserModel user = SessionUtil.getSessionUser(session);
			//图书保存路径
			String bookRootPath = FileUtil.getBookFilePath(request);
			//完整名称
			String fileFullName = file.getOriginalFilename();
			//文件新名称
			String newFileName = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
			//上传时的文件名称(不带后缀)
			String oldFileName = fileFullName.substring(0,fileFullName.lastIndexOf(Global.COMMA));
			//后缀
			String fileSuffix = fileFullName.substring(fileFullName.lastIndexOf(Global.COMMA)+1,fileFullName.length());
			String bookUrl =null;
			if(FileTypeEnum.PDF.name().equalsIgnoreCase(fileSuffix)){//pdf格式,直接替换名字，写入磁盘
				bookUrl = bookRootPath+File.separator+newFileName+Global.PDF_SUFFIX;
				file.transferTo(new File(bookUrl));
			}else{
				//写入磁盘
				bookUrl = bookRootPath+File.separator+fileFullName;
				file.transferTo(new File(bookUrl));
			}
			//转换成pdf之后的路径
			String bookPdfPath = createPdfBook(bookRootPath,fileFullName,fileSuffix,newFileName);
			book.setBookUrl(Global.BOOK_PATH+newFileName+Global.PDF_SUFFIX);
			book.setBookName(oldFileName);
			//图书封面地址
			book.setBookImg(Global.BOOK_PATH+newFileName+Global.IMG_SUFFIX);
			book.setCreater(user.getId());
			//获取第一页作为展示的图片
			createBookImg(bookPdfPath,newFileName,bookRootPath);
			bookService.saveOrUpdate(book);	
		} catch (Exception e) {
			logger.error("上传书籍异常",e);
		}
		return "redirect:bookList";
	}
	
	/**
	 * 生成图书缩略图
	 */
	private void createBookImg(String bookPdfPath,String newFileName,String bookRootPath){
		 PdfUtil.createScreenShoot(bookPdfPath, bookRootPath+File.separator+newFileName+Global.IMG_SUFFIX);
	}
	
	/**
	 * 统一转换成pdf，实际pdf路径
	 */
	private String createPdfBook(String bookRootPath,String fileFullName,String fileSuffix,String newFileName){
		//html新名称
		String htmlFilePath =  bookRootPath + File.separator + newFileName + Global.HTML_SUFFIX ;
		//pdf新名称
		String pdfFilePath =  bookRootPath + File.separator + newFileName + Global.PDF_SUFFIX ;
		if(FileTypeEnum.DOC.name().equalsIgnoreCase(fileSuffix)){
			PoiUtil.wordToHtml(bookRootPath+File.separator+fileFullName, htmlFilePath);
			PoiUtil.htmlToPDF(htmlFilePath, pdfFilePath);
		}else if(FileTypeEnum.XLS.name().equalsIgnoreCase(fileSuffix)){
			PoiUtil.excelToHtml(bookRootPath+File.separator+fileFullName, htmlFilePath);
			PoiUtil.htmlToPDF(htmlFilePath, pdfFilePath);
		}else if(FileTypeEnum.TXT.name().equalsIgnoreCase(fileSuffix)){
			PdfUtil.textToPdf(bookRootPath+File.separator+fileFullName, pdfFilePath);
		}else if(FileTypeEnum.PDF.name().equalsIgnoreCase(fileSuffix)){
			return pdfFilePath;
		}else{
			PdfUtil.picToPdf(bookRootPath+File.separator+fileFullName, pdfFilePath);
		}
		return pdfFilePath;
	}
	
}
